<?php

App::uses('AppController', 'Controller');

/**
 * Example Controller
 *
 * PHP version 5
 *
 * @category Controller
 * @package  Croogo
 * @version  1.0
 * @author   Fahad Ibnay Heylaal <contact@fahad19.com>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class ProvincesController extends AppController {

        
        public $components = array(
            'Search.Prg' => array(
                'presetForm' => array(
                    'paramType' => 'querystring',
                ),
                'commonProcess' => array(
                    'paramType' => 'querystring',
                    'filterEmpty' => true,
                ),
            ),
        );

        
        public $uses = array('Province');

        
        public function beforeFilter() {
                parent::beforeFilter();
               $this->Security->unlockedActions = array('getlist','getlist1');
        }

        
        public function admin_index() {
                $this->set('title_for_layout', __('Quận / Huyện'));
                //$this->Prg->commonProcess();
               // $searchFields = array('city_id', 'name');

                $this->Province->recursive = 0;
                $this->paginate = array(
                //'conditions' => $this->City->parseCriteria($this->passedArgs),
                'limit' => 20,
                'order' => array(
                        'Province.id' => 'asc'
                     )
                   );
               // $this->set('provinces', $this->paginate($this->Province->parseCriteria($this->passedArgs)));
                $this->set('cities', $this->Province->City->find('list'));
               // $this->set('displayFields', $this->Province->displayFields());
                //$this->set('searchFields', $searchFields);
                $Province = $this->paginate('Province');
                $this->set('provinces', $Province);
                
        }

       
        public function admin_add() {
                $this->set('title_for_layout', __('Add Province'));

                if (!empty($this->request->data)) {
                        $this->Province->create();
                        if ($this->Province->save($this->request->data)) {
                                $this->Session->setFlash(__('The Province has been saved'), 'default', array('class' => 'success'));
                                $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The Province could not be saved. Please, try again.'), 'default', array('class' => 'error'));
                        }
                }
                $cities = $this->Province->City->find('list');
                $this->set(compact('cities'));
                
        }

        
        public function admin_edit($id = null) {
                $this->set('title_for_layout', __('Edit Province'));

                if (!$id && empty($this->request->data)) {
                        $this->Session->setFlash(__('Invalid Province'), 'default', array('class' => 'error'));
                        $this->redirect(array('action' => 'index'));
                }
                if (!empty($this->request->data)) {
                        if ($this->Province->save($this->request->data)) {
                                $this->Session->setFlash(__('The Province has been saved'), 'default', array('class' => 'success'));
                                $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The Province could not be saved. Please, try again.'), 'default', array('class' => 'error'));
                        }
                }
                if (empty($this->request->data)) {
                        $this->request->data = $this->Province->read(null, $id);
                }
                $cities = $this->Province->City->find('list');
                $this->set(compact('cities'));                
               
        }

        
        public function admin_delete($id = null) {
                if (!$id) {
                        $this->Session->setFlash(__('Invalid id for Province'), 'default', array('class' => 'error'));
                        $this->redirect(array('action' => 'index'));
                }
                if ($this->Province->delete($id)) {
                        $this->Session->setFlash(__('Province deleted'), 'default', array('class' => 'success'));
                        $this->redirect(array('action' => 'index'));
                }
        }

        
        public function index() {
                $this->set('title_for_layout', __('Countries'));
                $this->set('exampleVariable', 'value here');
        }
        
       
        public function getlist1() {
                $this->autoRender = false;
                Configure::write('debug', 0);
                $provinces = $this->Province->find('all',array('conditions'=>array('Province.city_id'=>$this->request->data['city_id'])));
                $return = '';
                $i =1;
                foreach($provinces as $province):
                        $i=$i+1;
                    if (($i % 3) == 1)
                    
                        $return .= '<div class="checkbox"><input type="checkbox" name="data[Tutor][province_teach][]" value="'.$province['Province']['name'].'" id="TutorProvinceTeach'.$province['Province']['id'].'" /><label for="TutorProvinceTeach'.$province['Province']['id'].'">'.$province['Province']['name'].'</label></div> <br>';
                    
                    else $return .= '<div class="checkbox"><input type="checkbox" name="data[Tutor][province_teach][]" value="'.$province['Province']['name'].'" id="TutorProvinceTeach'.$province['Province']['id'].'" /><label for="TutorProvinceTeach'.$province['Province']['id'].'">'.$province['Province']['name'].'</label></div>' ;
                endforeach;
                 return $return;
        }

         public function getlist() {
                $this->autoRender = false;
                Configure::write('debug', 0);
                $provinces = $this->Province->find('all',array('conditions'=>array('Province.city_id'=>$this->request->data['city_id'])));
                $return = '<option value="">--Quận/Huyện--</option>';
                $i =1;
                foreach($provinces as $province):
                       
                    
                        $return .=  '<option value="'.$province['Province']['id'].'">'.$province['Province']['name'].'</option>';
                    
                    
                endforeach;
                 return $return;
        }   

       
        

}

